package com.example.ivory.accountbook.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.ivory.accountbook.Activity.AddItem;
import com.example.ivory.accountbook.Activity.SecondEvent;
import com.example.ivory.accountbook.Activity.Test;
import com.example.ivory.accountbook.Activity.update;
import com.example.ivory.accountbook.Activity.budget;
import com.example.ivory.accountbook.uilt.MyAdspter;
import com.example.ivory.accountbook.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Created by Ivory on 2016/8/27.
 */
public class Fragment_details extends basefragment implements View.OnClickListener {
    private View mContent;
    private List<Map<String,Object>> list;

    private MyAdspter myAdspter;
    private Button calendarbtn;
    private Button yusuanbtn;
    private Button updatebtn;
    private ListView listView=null;
    private String msg;

    private static int flag=0;

    public List<Map<String,Object>> getData(){
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
        for(int i=1;i<13;i++)
        {
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("left","收入9000"+i);
            map.put("image","t"+i);

            map.put("right","支出9000"+i);
            list.add(map);
        }

        return list;
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContent = inflater.inflate(R.layout.fragment_details, container, false);
        calendarbtn = (Button) mContent.findViewById(R.id.calendar);
        yusuanbtn = (Button) mContent.findViewById(R.id.yusuanbtn);
        updatebtn = (Button) mContent.findViewById(R.id.update);
        listView = (ListView) mContent.findViewById(R.id.id_listview);
        return mContent;
    }

    @Override
    protected void initData() {
        super.initData();



    }
    @Override
    public void onDestroy(){
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        flag=0;
    }
    @Override
    protected void initEvent() {
        if(flag==0){
            EventBus.getDefault().register(this);
            flag=1;
        }

        updatebtn.setOnClickListener(this);
        calendarbtn.setOnClickListener(this);
        yusuanbtn.setOnClickListener(this);
        listView.setAdapter(myAdspter);
        list = getData();
        myAdspter = new MyAdspter(mActivity, list);
        listView.setAdapter(myAdspter);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                switch (scrollState){
                    case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:



                        for (int t = 0; t < (listView.getCount()<7?listView.getCount():7); t++) {
                            ((listView.getChildAt(t).findViewById(R.id.leftbtn))).setVisibility(View.INVISIBLE);
                            ((listView.getChildAt(t).findViewById(R.id.rightbtn))).setVisibility(View.INVISIBLE);
                            myAdspter.notifyDataSetChanged();
                        }



                        break;
                    case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
                        for (int t = 0; t < (listView.getCount()<7?listView.getCount():7); t++) {
                            ((listView.getChildAt(t).findViewById(R.id.leftbtn))).setVisibility(View.INVISIBLE);
                            ((listView.getChildAt(t).findViewById(R.id.rightbtn))).setVisibility(View.INVISIBLE);
                            myAdspter.notifyDataSetChanged();
                        }
                        break;
                    case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                        for (int t = 0; t < (listView.getCount()<7?listView.getCount():7); t++) {
                            ((listView.getChildAt(t).findViewById(R.id.leftbtn))).setVisibility(View.INVISIBLE);
                            ((listView.getChildAt(t).findViewById(R.id.rightbtn))).setVisibility(View.INVISIBLE);
                            myAdspter.notifyDataSetChanged();
                        }
                        break;
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {

            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int i, final long l) {
                for (int t = 0; t < (listView.getCount()<7?listView.getCount():7); t++) {
                    ((listView.getChildAt(t).findViewById(R.id.leftbtn))).setVisibility(View.INVISIBLE);
                    ((listView.getChildAt(t).findViewById(R.id.rightbtn))).setVisibility(View.INVISIBLE);
                    myAdspter.notifyDataSetChanged();
                    (listView.getChildAt(t).findViewById(R.id.rightbtn)).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent=new Intent(mActivity,AddItem.class);
                            startActivity(intent);
                        }
                    });
                    (listView.getChildAt(t).findViewById(R.id.leftbtn)).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                           list.remove(i);
                            System.out.println(list.toArray().toString());
                            System.out.println(i);
                            System.out.println(l);

///                            myAdspter = new MyAdspter(mActivity, list);
                            myAdspter.notifyDataSetChanged();
                            listView.setAdapter(myAdspter);

                        }
                    });
                }
                view.findViewById(R.id.leftbtn).setVisibility(View.VISIBLE);
                view.findViewById(R.id.rightbtn).setVisibility(View.VISIBLE);

            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.update:
                Intent Up=new Intent(mActivity,update.class);
                startActivity(Up);
                break;
            case R.id.calendar:
                Intent Ca=new Intent(mActivity,Test.class);
                startActivity(Ca);
                break;
            case R.id.yusuanbtn:
                Intent Yu=new Intent(mActivity,budget.class);
                startActivity(Yu);
                break;
        }
    }
    public void onEventMainThread(SecondEvent event){
        msg=event.getMsg();
        Toast.makeText(getContext(), msg, Toast.LENGTH_LONG).show();
    }
}
